package net.gamebookengine.viewer;

import java.io.IOException;

import net.gamebookengine.viewer.helper.ThemeHelper;

import roboguice.activity.RoboActivity;
import roboguice.inject.InjectView;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.inject.Inject;

public class TitleActivity extends RoboActivity
{
	@InjectView(R.id.title_linearlayout) LinearLayout	linearLayout;
	@InjectView(R.id.book_title) TextView				bookTitle;
	@InjectView(R.id.book_summary) TextView				bookSummary;
	@InjectView(R.id.author_name) TextView				authorName;
	@InjectView(R.id.author_email) TextView				authorEmail;
	@InjectView(R.id.buttonOpenBook) TextView			buttonOpenBook;

	@Inject ThemeHelper									themeHelper;
	@Inject IGamebookService							gamebookService;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_title);

		try
		{
			gamebookService.init(getAssets().open(String.format("gamebooks/%s.zip", StaticStorage.SelectedGamebook)));
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		
		themeHelper.applyTheme(gamebookService.getTheme(), linearLayout);
		
		bookTitle.setText(gamebookService.getTitle());
		bookSummary.setText(gamebookService.getSummary());
		authorName.setText(gamebookService.getAuthor().getName());
		authorEmail.setText(gamebookService.getAuthor().getEmail());
		
		buttonOpenBook.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				StaticStorage.CurrentSectionId = gamebookService.getStartSectionId();
				startActivity(new Intent(TitleActivity.this, SectionActivity.class));
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.activity_title, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		boolean result;
		
		switch (item.getItemId())
		{
			case R.id.menu_settings:
			{
				startActivity(new Intent(TitleActivity.this, SettingsActivity.class));
				result = true;
				break;
			}
			
			case R.id.menu_about:
			{
				startActivity(new Intent(TitleActivity.this, AboutActivity.class));
				result = true;
				break;
			}
			
			default:
			{
				result = super.onOptionsItemSelected(item);
				break;
			}
		}
		
		return result;
	}
}
